package cn.tt.core.listener;

import cn.tt.core.constant.SysConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.SpringApplicationEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.*;

/**
 * @Description 实现ApplicationListener类，用来监听整个Spring的启动、停止、重启、关闭
 *
 * 	初始化启动的类还有ServletContextListener、ServletContainerInitializer、
 *
 * @Date 2017/7/29 21:15
 * @Since v1.7
 * @Autor Nick
 */
public class TTApplicationListener implements ApplicationListener {

	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 监听事件
	 */
	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		// 在这里可以监听到Spring Boot的生命周期
		if (event instanceof ApplicationEnvironmentPreparedEvent) {
			// 初始化环境变量
			logger.info("==========初始化环境变量==============");
		} else if (event instanceof ApplicationPreparedEvent) {
			// 初始化完成
			logger.info("==========初始化完成==============");
		} else if (event instanceof ContextRefreshedEvent) {
			// 应用刷新
			logger.info("==========应用刷新==============");
		} else if (event instanceof ApplicationReadyEvent) {
			// 应用已启动完成
			logger.info("==========应用已启动完成==============");
			//打印系统信息
			SysConstant.printLoadMessage();
		} else if (event instanceof ContextStartedEvent) {
			// 应用启动，需要在代码动态添加监听器才可捕获
		} else if (event instanceof ContextStoppedEvent) {
			// 应用停止
			logger.info("==========应用停止==============");
		} else if (event instanceof ContextClosedEvent) {
			// 应用关闭
			logger.info("==========应用关闭==============");
		} else if(event instanceof ApplicationContextEvent) {

		} else if(event instanceof SpringApplicationEvent) {
			logger.info("==========TTApplicationListener 其他==============");
		}
	}
}
